package org.kjtc.ExportExcel;

import org.apache.poi.hssf.usermodel.*;
import org.apache.poi.ss.usermodel.Cell;
import org.apache.poi.ss.util.CellRangeAddress;
import org.kjtc.entity.BackLog;
import org.kjtc.web.Tools;

import javax.servlet.http.HttpServletResponse;
import java.io.OutputStream;
import java.net.URLEncoder;
import java.util.Collection;
import java.util.Date;
import java.util.List;

/**
 * Created by Administrator on 2017-12-14.
 */
public class ExportReportExcel<T> {

    public void export(String title, String[] headers, List<BackLog> listDone, List<BackLog> listUndone, OutputStream out) {
        HSSFWorkbook workbook = new HSSFWorkbook();
        HSSFSheet sheet = workbook.createSheet();

        HSSFCellStyle style_title = workbook.createCellStyle();
        style_title.setBorderBottom(HSSFCellStyle.BORDER_THIN);
        style_title.setBorderLeft(HSSFCellStyle.BORDER_THIN);
        style_title.setBorderRight(HSSFCellStyle.BORDER_THIN);
        style_title.setBorderTop(HSSFCellStyle.BORDER_THIN);
        style_title.setAlignment(HSSFCellStyle.ALIGN_CENTER);
        HSSFFont font_title = workbook.createFont();
        font_title.setFontName("宋体");
        font_title.setFontHeightInPoints((short) 20);
        style_title.setFont(font_title);

        HSSFCellStyle style_header = workbook.createCellStyle();
        style_header.setBorderBottom(HSSFCellStyle.BORDER_THIN);
        style_header.setBorderLeft(HSSFCellStyle.BORDER_THIN);
        style_header.setBorderRight(HSSFCellStyle.BORDER_THIN);
        style_header.setBorderTop(HSSFCellStyle.BORDER_THIN);
        style_title.setAlignment(HSSFCellStyle.ALIGN_LEFT);
        HSSFFont font_header = workbook.createFont();
        font_header.setFontName("宋体");
        font_header.setFontHeightInPoints((short) 16);
        style_header.setFont(font_header);

        HSSFCellStyle style0 = workbook.createCellStyle();
        style0.setBorderBottom(HSSFCellStyle.BORDER_THIN);
        style0.setBorderLeft(HSSFCellStyle.BORDER_THIN);
        style0.setBorderRight(HSSFCellStyle.BORDER_THIN);
        style0.setBorderTop(HSSFCellStyle.BORDER_THIN);
        HSSFFont font0 = workbook.createFont();
        font0.setFontName("宋体");
        font0.setFontHeightInPoints((short) 18);
        style0.setFont(font0);

        HSSFCellStyle style = workbook.createCellStyle();
        style.setBorderBottom(HSSFCellStyle.BORDER_THIN);
        style.setBorderLeft(HSSFCellStyle.BORDER_THIN);
        style.setBorderRight(HSSFCellStyle.BORDER_THIN);
        style.setBorderTop(HSSFCellStyle.BORDER_THIN);
        style_title.setAlignment(HSSFCellStyle.ALIGN_LEFT);
        HSSFFont font = workbook.createFont();
        font.setFontName("宋体");
        font.setFontHeightInPoints((short) 14);
        style.setFont(font);

        HSSFPrintSetup ps = sheet.getPrintSetup();
        ps.setPaperSize(HSSFPrintSetup.A4_PAPERSIZE);

        HSSFRow row = sheet.createRow(0);
        HSSFCell cell = null;
        CellRangeAddress cellRangeAddress = null;
        for (short i = 0; i < headers.length; i++) {
            cell = row.createCell(i);
            cell.setCellStyle(style_title);
            cell.setCellValue(title);
        }
        cellRangeAddress = new CellRangeAddress(0, 0, 0, headers.length - 1);
        sheet.addMergedRegion(cellRangeAddress);

        row = sheet.createRow(1);
        for (short i = 0;i < headers.length;i++) {
            cell = row.createCell(i);
            cell.setCellStyle(style0);
            cell.setCellValue("完成BackLog");
        }
        cellRangeAddress = new CellRangeAddress(1, 1, 0, headers.length - 1);
        sheet.addMergedRegion(cellRangeAddress);

        row = sheet.createRow(2);
        for (short i = 0;i < headers.length;i++) {
            cell = row.createCell(i);
            cell.setCellStyle(style_header);
            cell.setCellValue(headers[i]);
        }

        for (int i = 0;i < listDone.size();i++) {
            BackLog backLog = listDone.get(i);
            row = sheet.createRow(3 + i);

            cell = row.createCell(0);
            cell.setCellStyle(style);
            cell.setCellValue(backLog.getProductName());

            cell = row.createCell(1);
            cell.setCellStyle(style);
            cell.setCellValue(backLog.getProductVersionName());

            cell = row.createCell(2);
            cell.setCellStyle(style);
            cell.setCellValue(backLog.getModuleName());

            cell = row.createCell(3);
            cell.setCellStyle(style);
            cell.setCellValue(backLog.getBackLogName());

            cell = row.createCell(4);
            cell.setCellStyle(style);
            cell.setCellValue(backLog.getExpectedWorkDate());

            cell = row.createCell(5);
            cell.setCellStyle(style);
            cell.setCellValue(backLog.getStateName());
        }

        row = sheet.createRow(listDone.size() + 3);
        for (short i = 0;i < headers.length;i++) {
            cell = row.createCell(i);
            cell.setCellStyle(style0);
            cell.setCellValue("未完成BackLog");
        }
        cellRangeAddress = new CellRangeAddress(listDone.size() + 3, listDone.size() + 3, 0, headers.length - 1);
        sheet.addMergedRegion(cellRangeAddress);

        row = sheet.createRow(listDone.size() + 4);
        for (short i = 0;i < headers.length;i++) {
            cell = row.createCell(i);
            cell.setCellStyle(style_header);
            cell.setCellValue(headers[i]);
        }

        for (int i = 0;i < listUndone.size();i++) {
            BackLog backLog = listUndone.get(i);
            row = sheet.createRow(listDone.size() + 5 + i);

            cell = row.createCell(0);
            cell.setCellStyle(style);
            cell.setCellValue(backLog.getProductName());

            cell = row.createCell(1);
            cell.setCellStyle(style);
            cell.setCellValue(backLog.getProductVersionName());

            cell = row.createCell(2);
            cell.setCellStyle(style);
            cell.setCellValue(backLog.getModuleName());

            cell = row.createCell(3);
            cell.setCellStyle(style);
            cell.setCellValue(backLog.getBackLogName());

            cell = row.createCell(4);
            cell.setCellStyle(style);
            cell.setCellValue(backLog.getExpectedWorkDate());

            cell = row.createCell(5);
            cell.setCellStyle(style);
            cell.setCellValue(backLog.getStateName());
        }
        for (int i = 0;i < headers.length;i++) {
            sheet.autoSizeColumn(i, true);
        }
        try {
            workbook.write(out);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    public void run (List<BackLog> listDone, List<BackLog> listUndone, HttpServletResponse response, Date txtBeginDate) {
        String[] headers = {"产品", "版本", "功能", "BackLog", "工时(人日)", "进度"};
        String title = Tools.convertDate(txtBeginDate);
        try {
            response.setCharacterEncoding("UTF-8");
            response.setContentType("application/x-download");
            String fileName = "周报.xls";
            fileName = URLEncoder.encode(fileName, "UTF-8");
            response.addHeader("Content-Disposition", "attachment;fileName=" + fileName);
            OutputStream out = response.getOutputStream();
            export(title, headers, listDone, listUndone, out);
            out.flush();
            out.close();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}
